<?php

/** 
 * RedXenon Database (RXDatabaseConnection.php)
 * Class to manage Database Connection
 * 
 * @category RedXenon
 * @package RedXenon_Database
 * @copyright Copyright (c) 2011 Red Xenon (http://www.redxenon.com)
 * @version $Id: RXDatabaseConnection.php 1.1 2012-06-30 $ 
 */
class RXDatabaseConnection{
	
	private $host;
	private $username;
	private $password;
	private $database;
	private $connection;
	private $error;
	
	function __construct($configurationFile = NULL, $configurationCategory = NULL){
		if(!$configurationFile){
			if(@file_exists("../../conf/database.inc")){
				require_once("../../conf/database.inc");
				$this->host = HOST;
				$this->username = USERNAME;
				$this->password = PASSWORD; 
				$this->database = DATABASE;
				$this->connect();
			}
		}else{
			if(substr($configurationFile, -3) == 'ini'){		
				require_once("File/RXFileManagerINI.php");
				$iniFileManager = new RXFileManagerINI($configurationFile);
				$data = $iniFileManager->getData($configurationCategory);
				if(is_array($data)){
					$this->host = $data["HOST"];
					$this->username = $data["USERNAME"];
					$this->password = $data["PASSWORD"];
					$this->database = $data["DATABASE"];
					$this->connect();
				}
			}else if(substr($configurationFile, -3) == 'inc'){
				if(@file_exists($configurationFile)){
					require_once($configurationFile);
					$this->host = HOST;
					$this->username = USERNAME;
					$this->password = PASSWORD;
					$this->database = DATABASE;
					$this->connect();
				}
			}
		}
	}
	
	function __destruct(){
		$this->disconnect();
	}
	
	/**
	 * Connects the database connection
	 * @throws Exception in case of error
	 */
	function connect(){
		/* If host is not defined, localhost is used */
		if(empty($this->host)){
			self::setHost("localhost");
		}
		
		/* If username is not defined, root is used */
		if(empty($this->username)){
			self::setUsername("root");
		}
		
		if(empty($this->database)){
			throw new Exception("Database name not specified".PHP_EOL);
		}
		
		$this->connection = new mysqli($this->host, $this->username, $this->password, $this->database);
		if($this->connection->connect_errno){
			$message = $this->connection->connect_errno.": ".$this->connection->connect_error.PHP_EOL;
			unset($this->connection);
			throw new Exception($message);	
		}
	}
	
	/**
	 * Disconnects the database connection
	 * @throws Exception if connection cannot be terminated
	 */
	function disconnect(){
		if($this->connection && !$this->connection->close()){
			throw new Exception("Database connection could not be terminated".PHP_EOL);
		}else{
			unset($this->connection);	
		}		
	}
	
	/**
	 * Start a transaction towards the database
	 */
	function startTransaction(){
		if($this->connection){
			$this->connection->autocommit(FALSE);
		}
	} 
	
	/**
	 * Commits a transaction
	 */
	function commit(){
		if($this->connection){
			$this->connection->commit();
		}
	}
	
	/**
	 * Rollbacks a transaction
	 */
	function rollback(){
		if($this->connection){
			$this->connection->rollback();
		}
	}
	
	/**
	 * Prepeares a statement query
	 * @param String $query
	 */
	function prepeareStatement($query){
		if($this->connection){
			return $this->connection->prepare($query);
		}else{
			return FALSE;
		}
	}	
	
	/**
	 * Loads the configuration parameters
	 * @param String $host
	 * @param String $username
	 * @param String $password
	 * @param String $database
	 */
	function setConfiguration($host, $username, $password, $database){
		self::setHost($host);
		self::setUsername($username);
		self::setPassword($password);
		self::setDatabase($database);
	}
	
	/**
	 * Loads a new value into the host name variable
	 * related to the database connection
	 * @param String $host
	 */
	function setHost($host){
		$this->host = $host;
	}
	
	/**
	 * Loads a new value into the username variable
	 * related to the database connection
	 * @param String $username
	 */
	function setUsername($username){
		$this->username = $username;
	}
	
	/**
	 * Loads a new value into the password variable
	 * related to the database connection
	 * @param String $password
	 */
	function setPassword($password){
		$this->password = $password;
	}
	
	/**
	 * Loads a new value into the database name variable
	 * related to the database connection
	 * @param String $database
	 */
	function setDatabase($database){
		$this->database = $database;
	}
	
	/**
	 * @return the host name value 
	 */
	function getHost(){
		return $this->host;
	}
	
	/**
	 * @return the username value
	 */
	function getUsername(){
		return $this->username;
	}
	
	/**
	 * @return the password value
	 */
	function getPassword(){
		return $this->password;
	}
	
	/**
	 * @return the connection stablished
	 */
	function getConnection(){
		return $this->connection;
	}
	
	/**
	 * @return the last inserted id
	 */
	function getLastInsertedId(){
		return $this->connection->insert_id;
	}
	
	/**
	 * @return Result
	 */
	function exec($query){
		$ret = $this->connection->query($query);
		if(!$ret){
			$this->error = "Error ".$this->connection->errno.": ".$this->connection->error." on Query ".$query.PHP_EOL;
			return FALSE;
		}else{
			return $ret;
		}
	}
	
	function getError(){
		return $this->error;
	}
}
?>